#ifndef MPHTTPHEADERS_H
#define MPHTTPHEADERS_H

#include <string>
#include <vector>


/******************************
 *
 * 名   称：   Http请求头
 * 用   途：   定义Http请求头相关属性与方法。
 *            >> HttpHeader 为基类。定义四个Request和Response公用的属性（connection_keepalive、cache_control、pragma、expires）相关操作以及其他自定义属性的操作
 *            >> HttpRequestHeaders 派生于HttpHeader，针对Http Request增加属性（authorization、accept、userAgent）相关操作
 *            >> HttpResponseHeaders 派生于HttpHeader，针对Http Response
 * 作用范围：   公共的
 * 创建时间：   2022-10-03
 *
 * 参考：
 * https://blog.csdn.net/qq_34039018/article/details/100077763                  [用于std::find函数进行查询]
 * https://blog.csdn.net/qq_42695024/article/details/125406129
 * https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Accept-Encoding    [Web 开发技术HTTP Headers]
 *
 ******************************/

namespace MP{
namespace HTTP{

typedef struct HttpHeader {
    HttpHeader(std::string k,
               std::string v)
    {
        key=k;
        value = v;
    }

    //用于std::find函数进行查询
    //参考：https://blog.csdn.net/qq_34039018/article/details/100077763
    //     https://blog.csdn.net/qq_42695024/article/details/125406129
    bool operator == (const std::string & k){
        return (this->key == k);
    }

    std::string key;
    std::string value;

} HttpHeader,*p_HttpHeader;

/*****************************************
 * HttpHeaders
 *****************************************/

class HttpHeaders
{
public:
    HttpHeaders();
    virtual ~HttpHeaders() = default;

    void addHeader(const std::string & key,
                   const std::string & val);
    void remove(const std::string & key);
    void clearHeaders();

    std::string getHeader(const std::string & key);
    const std::vector<HttpHeader> & data() const;

    void setConnectionKeepalive(bool keepalive);
    bool getConnectionKeepalive() const;

    void setNoCache();
    //例如：setCache_Control("max-age=60")
    void setCache_Control(const std::string & cache_control);

    const std::string& getCache_Control() const;
    const std::string& getPragma() const;
    const std::string& getExpires() const;

protected:
    virtual bool checkIsCommonHttpHeader(const std::string & key,
                                         const std::string & val);
protected:
    //Req、Resp均包括的Header
    bool _connection_keepalive;                                          //是否保持HTTP连接
    //缓存设置 参考：https://blog.csdn.net/fengzibs/article/details/85321530
    std::string _cache_control;
    std::string _pragma;
    std::string _expires;

    //自定义Header
    std::vector<HttpHeader> _headers;
};

/*****************************************
 * HttpRequestHeaders
 *****************************************/

class HttpRequestHeaders  : public HttpHeaders
{
public:
    HttpRequestHeaders() : HttpHeaders() { }

    // Set the Authorization header. E.g. Authorization:hmac id="xxxxxxx", algorithm="hmac-sha1", headers="source x-date", signature="xyxyxyxyxyxy"
    void setAuthorization(const std::string & authorization);
    const std::string& getAuthorization() const;

    void setAccept(const std::string & accept);
    const std::string& getAccept() const;

    void setUserAgent(const std::string & userAgent);
    const std::string& getUserAgent() const;

    void setAcceptEncoding(const std::string & accept_encoding);
    const std::string& getAcceptEncoding() const;

protected:
    virtual bool checkIsCommonHttpHeader(const std::string & key,
                                         const std::string & val) override;

protected:
    std::string _authorization;                         //认证信息
    std::string _accept = "*/*";                        //接收的返回值类型
    std::string _accept_encoding = "identity";          //支持的编码类型 gzip identity
    std::string _userAgent = "boost.asio.mapleleaf";    //客户端信息
};


/*****************************************
 * HttpResponseHeaders
 *****************************************/

class HttpResponseHeaders  : public HttpHeaders{
public:
    HttpResponseHeaders() : HttpHeaders() { }
};

}   //HTTP
}   //MP



#endif // MPHTTPHEADERS_H
